home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / DirectPlay / Conferencer / frmChat.frm next >
Text File  |  2001-10-08  |  10KB  |  263 lines

  1. VERSION 5.00
  2. Begin VB.Form frmChat 
  3.    BorderStyle     =   3  'Fixed Dialog
  4.    Caption         =   "vbDirectPlay Chat"
  5.    ClientHeight    =   5085
  6.    ClientLeft      =   45
  7.    ClientTop       =   330
  8.    ClientWidth     =   7710
  9.    Icon            =   "frmChat.frx":0000
  10.    LinkTopic       =   "Form1"
  11.    MaxButton       =   0   'False
  12.    MinButton       =   0   'False
  13.    ScaleHeight     =   5085
  14.    ScaleWidth      =   7710
  15.    StartUpPosition =   3  'Windows Default
  16.    Begin VB.CommandButton cmdWhisper 
  17.       Caption         =   "Whisper"
  18.       Height          =   255
  19.       Left            =   5820
  20.       TabIndex        =   3
  21.       Top             =   4740
  22.       Width           =   1695
  23.    End
  24.    Begin VB.Timer tmrUpdate 
  25.       Enabled         =   0   'False
  26.       Interval        =   50
  27.       Left            =   10200
  28.       Top             =   120
  29.    End
  30.    Begin VB.TextBox txtSend 
  31.       Height          =   285
  32.       Left            =   60
  33.       TabIndex        =   0
  34.       Top             =   4740
  35.       Width           =   5655
  36.    End
  37.    Begin VB.ListBox lstUsers 
  38.       Height          =   4545
  39.       Left            =   5760
  40.       TabIndex        =   2
  41.       Top             =   120
  42.       Width           =   1815
  43.    End
  44.    Begin VB.TextBox txtChat 
  45.       Height          =   4635
  46.       Left            =   60
  47.       Locked          =   -1  'True
  48.       MultiLine       =   -1  'True
  49.       ScrollBars      =   2  'Vertical
  50.       TabIndex        =   1
  51.       TabStop         =   0   'False
  52.       Top             =   60
  53.       Width           =   5595
  54.    End
  55. End
  56. Attribute VB_Name = "frmChat"
  57. Attribute VB_GlobalNameSpace = False
  58. Attribute VB_Creatable = False
  59. Attribute VB_PredeclaredId = True
  60. Attribute VB_Exposed = False
  61. Option Explicit
  62. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  63. '
  64. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  65. '
  66. '  File:       frmChat.frm
  67. '
  68. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  69. Implements DirectPlay8Event
  70.  
  71. Private Sub cmdWhisper_Click()
  72.     Dim lMsg As Long, lOffset As Long
  73.     Dim sChatMsg As String
  74.     Dim oBuf() As Byte
  75.     
  76.     If lstUsers.ListIndex < 0 Then
  77.         MsgBox "You must select a user in the list before you can whisper to that person.", vbOKOnly Or vbInformation, "Select someone"
  78.         Exit Sub
  79.     End If
  80.     
  81.     If lstUsers.ItemData(lstUsers.ListIndex) = 0 Then
  82.         MsgBox "Why are you whispering to yourself?", vbOKOnly Or vbInformation, "Select someone else"
  83.         Exit Sub
  84.     End If
  85.     
  86.     If txtSend.Text = vbNullString Then
  87.         MsgBox "What's the point of whispering if you have nothing to say..", vbOKOnly Or vbInformation, "Enter text"
  88.         Exit Sub
  89.     End If
  90.         
  91.     'Send this message to the person you are whispering to
  92.     lMsg = MsgWhisper
  93.     lOffset = NewBuffer(oBuf)
  94.     AddDataToBuffer oBuf, lMsg, LenB(lMsg), lOffset
  95.     sChatMsg = txtSend.Text
  96.     AddStringToBuffer oBuf, sChatMsg, lOffset
  97.     txtSend.Text = vbNullString
  98.     dpp.SendTo lstUsers.ItemData(lstUsers.ListIndex), oBuf, 0, DPNSEND_NOLOOPBACK
  99.     UpdateChat "**<" & gsUserName & ">** " & sChatMsg
  100.     
  101. End Sub
  102.  
  103. Private Sub Form_Load()
  104.     'load all of the players into our list
  105.     LoadAllPlayers
  106. End Sub
  107.  
  108. Private Sub UpdateChat(ByVal sString As String)
  109.     'Update the chat window first
  110.     txtChat.Text = txtChat.Text & sString & vbCrLf
  111.     'Now limit the text in the window to be 16k
  112.     If Len(txtChat.Text) > 16384 Then
  113.         txtChat.Text = Right$(txtChat.Text, 16384)
  114.     End If
  115.     'Autoscroll the text
  116.     txtChat.SelStart = Len(txtChat.Text)
  117. End Sub
  118.  
  119. Private Sub txtSend_KeyPress(KeyAscii As Integer)
  120.     Dim lMsg As Long, lOffset As Long
  121.     Dim sChatMsg As String
  122.     Dim oBuf() As Byte
  123.     
  124.     If KeyAscii = vbKeyReturn Then
  125.         KeyAscii = 0
  126.         If txtSend.Text = vbNullString Then Exit Sub
  127.         'Send this message to everyone
  128.         lMsg = MsgChat
  129.         lOffset = NewBuffer(oBuf)
  130.         AddDataToBuffer oBuf, lMsg, LenB(lMsg), lOffset
  131.         sChatMsg = txtSend.Text
  132.         AddStringToBuffer oBuf, sChatMsg, lOffset
  133.         txtSend.Text = vbNullString
  134.         dpp.SendTo DPNID_ALL_PLAYERS_GROUP, oBuf, 0, DPNSEND_NOLOOPBACK
  135.         UpdateChat "<" & gsUserName & ">" & sChatMsg
  136.     End If
  137. End Sub
  138.  
  139. Private Function GetName(ByVal lID As Long) As String
  140.     Dim lCount As Long
  141.     
  142.     GetName = vbNullString
  143.     For lCount = 0 To lstUsers.ListCount - 1
  144.         If lstUsers.ItemData(lCount) = lID Then 'This is the player
  145.             GetName = lstUsers.List(lCount)
  146.             Exit For
  147.         End If
  148.     Next
  149. End Function
  150.  
  151. Public Sub LoadAllPlayers()
  152.     Dim lCount As Long
  153.     Dim dpPlayer As DPN_PLAYER_INFO
  154.     
  155.     lstUsers.Clear
  156.     For lCount = 1 To dpp.GetCountPlayersAndGroups(DPNENUM_PLAYERS)
  157.         dpPlayer = dpp.GetPeerInfo(dpp.GetPlayerOrGroup(lCount))
  158.         lstUsers.AddItem dpPlayer.Name
  159.         If ((dpPlayer.lPlayerFlags And DPNPLAYER_LOCAL) <> DPNPLAYER_LOCAL) Then
  160.             'Do not add a ItemData key for myself
  161.             lstUsers.ItemData(lstUsers.ListCount - 1) = dpp.GetPlayerOrGroup(lCount)
  162.         End If
  163.     Next
  164. End Sub
  165.  
  166. Private Sub DirectPlay8Event_AddRemovePlayerGroup(ByVal lMsgID As Long, ByVal lPlayerID As Long, ByVal lGroupID As Long, fRejectMsg As Boolean)
  167.     'VB requires that we must implement *every* member of this interface
  168. End Sub
  169.  
  170. Private Sub DirectPlay8Event_AppDesc(fRejectMsg As Boolean)
  171.     'VB requires that we must implement *every* member of this interface
  172. End Sub
  173.  
  174. Private Sub DirectPlay8Event_AsyncOpComplete(dpnotify As DxVBLibA.DPNMSG_ASYNC_OP_COMPLETE, fRejectMsg As Boolean)
  175.     'VB requires that we must implement *every* member of this interface
  176. End Sub
  177.  
  178. Private Sub DirectPlay8Event_ConnectComplete(dpnotify As DxVBLibA.DPNMSG_CONNECT_COMPLETE, fRejectMsg As Boolean)
  179.     'VB requires that we must implement *every* member of this interface
  180. End Sub
  181.  
  182. Private Sub DirectPlay8Event_CreateGroup(ByVal lGroupID As Long, ByVal lOwnerID As Long, fRejectMsg As Boolean)
  183.     'VB requires that we must implement *every* member of this interface
  184. End Sub
  185.  
  186. Private Sub DirectPlay8Event_CreatePlayer(ByVal lPlayerID As Long, fRejectMsg As Boolean)
  187.     'VB requires that we must implement *every* member of this interface
  188. End Sub
  189.  
  190. Private Sub DirectPlay8Event_DestroyGroup(ByVal lGroupID As Long, ByVal lReason As Long, fRejectMsg As Boolean)
  191.     'VB requires that we must implement *every* member of this interface
  192. End Sub
  193.  
  194. Private Sub DirectPlay8Event_DestroyPlayer(ByVal lPlayerID As Long, ByVal lReason As Long, fRejectMsg As Boolean)
  195.     Dim lCount As Long
  196.     
  197.     'We only care when someone leaves.  When they join we will receive a 'MSGJoin'
  198.     'Remove this player from our list
  199.     For lCount = 0 To lstUsers.ListCount - 1
  200.         If lstUsers.ItemData(lCount) = lPlayerID Then 'This is the player
  201.             UpdateChat "---- " & lstUsers.List(lCount) & " has left the chat."
  202.             lstUsers.RemoveItem lCount
  203.             Exit For
  204.         End If
  205.     Next
  206. End Sub
  207.  
  208. Private Sub DirectPlay8Event_EnumHostsQuery(dpnotify As DxVBLibA.DPNMSG_ENUM_HOSTS_QUERY, fRejectMsg As Boolean)
  209.     'VB requires that we must implement *every* member of this interface
  210. End Sub
  211.  
  212. Private Sub DirectPlay8Event_EnumHostsResponse(dpnotify As DxVBLibA.DPNMSG_ENUM_HOSTS_RESPONSE, fRejectMsg As Boolean)
  213.     'VB requires that we must implement *every* member of this interface
  214. End Sub
  215.  
  216. Private Sub DirectPlay8Event_HostMigrate(ByVal lNewHostID As Long, fRejectMsg As Boolean)
  217.     'VB requires that we must implement *every* member of this interface
  218. End Sub
  219.  
  220. Private Sub DirectPlay8Event_IndicateConnect(dpnotify As DxVBLibA.DPNMSG_INDICATE_CONNECT, fRejectMsg As Boolean)
  221.     'VB requires that we must implement *every* member of this interface
  222. End Sub
  223.  
  224. Private Sub DirectPlay8Event_IndicatedConnectAborted(fRejectMsg As Boolean)
  225.     'VB requires that we must implement *every* member of this interface
  226. End Sub
  227.  
  228. Private Sub DirectPlay8Event_InfoNotify(ByVal lMsgID As Long, ByVal lNotifyID As Long, fRejectMsg As Boolean)
  229.     'VB requires that we must implement *every* member of this interface
  230. End Sub
  231.  
  232. Private Sub DirectPlay8Event_Receive(dpnotify As DxVBLibA.DPNMSG_RECEIVE, fRejectMsg As Boolean)
  233.     'process what msgs we receive.
  234.     'All we care about in this form is what msgs we receive.
  235.     Dim lMsg As Long, lOffset As Long
  236.     Dim dpPeer As DPN_PLAYER_INFO, sName As String
  237.     Dim sChat As String
  238.     
  239.     With dpnotify
  240.     GetDataFromBuffer .ReceivedData, lMsg, LenB(lMsg), lOffset
  241.     Select Case lMsg
  242.     Case MsgChat
  243.         sName = GetName(.idSender)
  244.         sChat = GetStringFromBuffer(.ReceivedData, lOffset)
  245.         UpdateChat "<" & sName & "> " & sChat
  246.     Case MsgWhisper
  247.         sName = GetName(.idSender)
  248.         sChat = GetStringFromBuffer(.ReceivedData, lOffset)
  249.         UpdateChat "**<" & sName & ">** " & sChat
  250.     End Select
  251.     End With
  252.     
  253. End Sub
  254.  
  255. Private Sub DirectPlay8Event_SendComplete(dpnotify As DxVBLibA.DPNMSG_SEND_COMPLETE, fRejectMsg As Boolean)
  256.     'VB requires that we must implement *every* member of this interface
  257. End Sub
  258.  
  259. Private Sub DirectPlay8Event_TerminateSession(dpnotify As DxVBLibA.DPNMSG_TERMINATE_SESSION, fRejectMsg As Boolean)
  260.     'VB requires that we must implement *every* member of this interface
  261. End Sub
  262.  
  263.